<template>
  <div>
    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search || data.name.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
    >
      <el-table-column label="班次" prop="airplane"> </el-table-column>
      <el-table-column label="余票" prop="ticket"> </el-table-column>
      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            v-if="!isPower && !isVisitor"
            @click="purchaseAirplane(scope.row)"
          >
            购买
          </el-button>
          <el-button
            size="mini"
            v-if="isPower"
            @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            v-if="isPower"
            @click="deleteUser(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加机票信息form弹窗-->
    <el-dialog
      title="修改/添加机票信息"
      center
      width="30%"
      :show-close="false"
      :visible.sync="dialogUserFormVisible"
      @closed="resetForm('airplaneForm')"
    >
      <el-form
        :model="airplaneForm"
        status-icon
        :rules="airplaneRules"
        ref="airplaneForm"
        label-position="left"
        label-width="60px"
        label-suffix=":"
      >
        <el-form-item label="班次" prop="airplane">
          <el-input
            maxlength="10"
            style="width: 170px"
            v-model="airplaneForm.airplane"
          ></el-input>
        </el-form-item>
        <el-form-item label="余票" prop="ticket">
          <el-input
            maxlength="10"
            style="width: 170px"
            v-model="airplaneForm.ticket"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addUpdateButton(airplaneForm)"
          >确 定</el-button
        >
        <el-button
          @click="
            resetForm('airplaneForm');
            dialogUserFormVisible = false;
          "
          >取 消</el-button
        >
      </div>
    </el-dialog>
    <el-row>
      <!-- 添加用户信息按钮 -->
      <el-col :span="2">
        <el-button
          style="margin-top: 10px"
          type="success"
          size="mini"
          v-if="isPower"
          @click="dialogUserFormVisible = true"
        >
          添加
        </el-button>
        <el-col :span="2" v-if="!isPower"
          ><div class="grid-content"></div
        ></el-col>
      </el-col>
      <!-- 分页按钮 -->
      <el-col :span="10" :offset="12">
        <div class="block">
          <el-pagination
            align="right"
            @current-change="handleCurrentChange"
            prev-text="上一页"
            next-text="下一页"
            :page-size="pageSize"
            :current-page="currentPage"
            layout=" prev, pager, next, jumper"
            :total="pageTotal"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageTotal: 0,
      currentPage: 1,
      pageSize: 13,
      isPower: false,
      isVisitor: true,
      dialogUserFormVisible: false,
      tableData: [
        {},
      ],
      airplaneForm: {},
      isUpdate: false,
      airplaneRules: {
        airplane: [{ required: true, message: "请输入班次", trigger: "blur" }],
        ticket: [{ required: true, message: "请输入余票量!", trigger: "blur" }],
      },
      search: "",
    };
  },
  methods: {
    // 编辑航班信息
    handleEdit(index, row) {
      // 这里不能直接this.form = row
      // 注意深浅拷贝
      this.isUpdate = true;
      this.airplaneForm = row;
      this.dialogUserFormVisible = true;
    },
    // 删除航班信息
    deleteUser(index, row) {
      console.log(index, row);
      this.$confirm("确认删除该航班?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http
          .post(
            "/server/airplane/deleteAirplane?airplane=" +
              row.airplane
          )
          .then((res) => {
            if (res.data.status) {
              this.$message({
                message: res.data.msg,
                type: "success",
              });
              setTimeout(() => {
                this.pageTotal--;
                this.t = parseInt((this.pageTotal - 1) / this.pageSize) + 1;
                this.currentPage =
                  this.currentPage <= this.t ? this.currentPage : this.t;
                this.findAll(this.currentPage, this.pageSize);
                this.currentPage = this.currentPage;
              }, 100);
            } else {
              this.$message.error(res.data.msg);
            }
          });
      });
    },
    // 修改当前页
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.findAll(currentPage, this.pageSize);
    },
    // 修改/添加机票
    addUpdateButton(table) {
      if (this.isUpdate) {
        this.postResponseTable(
          "/server/airplane/updateAirplane",
          table
        );
      } else {
        this.postResponseTable(
          "/server/airplane/addAirplane",
          table
        );
      }
      this.airplaneForm = {};
      this.dialogUserFormVisible = false;
      this.isUpdate = false;
      setTimeout(() => {
        this.findAll(this.currentPage, this.pageSize);
      }, 1000);
    },
    // 购买机票
    purchaseAirplane(row) {
      var table = {
        airplane: row.airplane,
        user: JSON.parse(window.sessionStorage.getItem("userInfo")).name,
      };
      this.postResponseTable(
        "/server/ticket/insertTicket",
        table
      );
      setTimeout(() => {
        this.findAll(this.currentPage, this.pageSize);
      }, 100);
    },
    // 发送请求，返回状态
    postResponseTable(url, table) {
      this.$http.post(url, table).then((res) => {
        if (res.data.status) {
          this.$message({
            message: res.data.msg,
            type: "success",
          });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.airplaneForm = {};
    },
    // 分页获取航班列表
    findAll(currentPage, pageSize) {
      currentPage = currentPage ? currentPage : 1;
      pageSize = pageSize ? pageSize : this.pageSize;
      var t = this.$http
        .get(
          "/server/airplane/findByPage?currentPage=" +
            currentPage +
            "&pageSize=" +
            pageSize
        )
        .then((res) => {
          this.tableData = res.data.user;
          this.pageTotal = res.data.pageTotal;
          console.log(t);
        });
    },
  },
  created() {
    var user = JSON.parse(window.sessionStorage.getItem("userInfo"));
    if (user !== null) {
      this.isVisitor = false;
      this.isPower = user.power ? true : false;
    }
    this.findAll();
  },
};
</script>
<style>
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
</style>
